package video.background.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import video.background.Controller.View.CompareResult;
import video.background.Controller.View.OptResultView;
import video.background.Dao.OptRecodeDao;
import video.background.Dao.OptResultDao;
import video.background.Dao.UserDao;
import video.background.Dao.UserGroupDao;
import video.background.Model.OptRecode;
import video.background.Model.OptResult;
import video.background.Model.UserGroup;
import video.background.Utils.JwtUtils;
import video.background.Utils.Result;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: video-backgroud
 * @description:
 * @author: hlt
 * @create: 2023-08-16 11:36
 **/
@RestController
@RequestMapping("/optRecode")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = "声纹历史记录")
public class OptRecodeController {

    @Autowired
    private UserGroupDao userGroupDao;

    @Autowired
    private OptRecodeDao optRecodeDao;

    @Autowired
    private OptResultDao optResultDao;

    @Autowired
    private UserDao userDao;

    @GetMapping("/getRecode")
    @ApiOperation("个人查询声纹操作历史记录")
    public Result getRecode(@RequestHeader String Authorization,
                            @RequestParam Long groupId,
                            @RequestParam(required = false) String username,
                            @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
                            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize){
        Long userId = JwtUtils.decoderToken(Authorization);
        if(userId == null || groupId == 0) return Result.Error("数据不全！", null);
        LambdaQueryWrapper<UserGroup> uglqw = new LambdaQueryWrapper<>();
        uglqw.eq(UserGroup::getGroupId,groupId).eq(UserGroup::getUserId,userId);
        UserGroup userGroup = userGroupDao.selectOne(uglqw);
        char userRole = userGroup.getUserRole();
        Page<OptResultView> optResultViewPage = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<OptRecode> olqw = new LambdaQueryWrapper<>();
        if(userRole == '2'){
            // 成员只能查询自己的操作记录
            olqw.eq(OptRecode::getUserId,userId).eq(OptRecode::getGroupId,groupId);
            if(username != null) olqw.like(OptRecode::getUserName,username);
        }else{
            // 管理员可以查看所有人的操作记录
            olqw.eq(OptRecode::getGroupId,groupId);
            if (username != null) olqw.like(OptRecode::getUserName,username);
        }
        List<OptResultView> optResultViews = optRecodeDao.selectList(olqw).stream().map(optRecode -> {
            OptResultView optResultView = new OptResultView();
            BeanUtils.copyProperties(optRecode, optResultView);
            if (optRecode.getOptId() == 4 || optRecode.getOptId() == 5) {
                LambdaQueryWrapper<OptResult> orlqw = new LambdaQueryWrapper<>();
                orlqw.eq(OptResult::getRecodeId,optRecode.getRecodeId());
                List<OptResult> optResults = optResultDao.selectList(orlqw);
                ArrayList<CompareResult> resultList = new ArrayList<>();
                for(OptResult item : optResults){
                    resultList.add(new CompareResult(item.getUserName(), item.getScore()));
                }
                optResultView.setResult(resultList);
            } else {
                optResultView.setResult(new ArrayList<CompareResult>());
            }
            return optResultView;
        }).collect(Collectors.toList());
        int count = optResultViews.size();
        Collections.reverse(optResultViews);
        optResultViewPage.setRecords(optResultViews);
        optResultViewPage.setTotal(count);
        optResultViewPage.setPages(count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
        return Result.Success("请求成功！", optResultViewPage);
    }
}
